package com.chuanyang.driverh.mainhome.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.mainhome.MoneyFollowWaybilListActivity;
import com.chuanyang.driverh.mainhome.WaybilDetailActivity;
import com.chuanyang.driverh.response2.ResultGoodsAllot;
import com.chuanyang.driverh.responseresult.LookWaybillData;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.eventbus.EventBusUtil;
import com.chuanyang.driverh.utils.eventbus.RefleshFragEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;


public class MoneyFollowWaybilListAdapter extends BaseListAdapter<LookWaybillData.DataBean> {//首页竞价大厅的adapter
    private Activity activity;
    private Dialog dialog;
    private String currency;

    public MoneyFollowWaybilListAdapter(Activity activity) {
        super(activity);//此处不传入集合，则默认集合长度为0，在setdataresult（）方法中，添加数据并刷新
        this.activity = activity;
        currency=((MoneyFollowWaybilListActivity)activity).getCurrency();
    }

    @Override
    public View getZkView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = getInflater().inflate(R.layout.home_item_money_follow_waybil_list, parent, false);
            holder = new ViewHolder();
            holder.iv_header = (ImageView) convertView.findViewById(R.id.iv_header);
            holder.tv_driver_name = (TextView) convertView.findViewById(R.id.tv_driver_name);
            holder.tv_car_band = (TextView) convertView.findViewById(R.id.tv_car_band);
            holder.tv_car_band_num = (TextView) convertView.findViewById(R.id.tv_car_band_num);
            holder.tv_waibil_no = (TextView) convertView.findViewById(R.id.tv_waibil_no);
            holder.tv_weight = (TextView) convertView.findViewById(R.id.tv_weight);
            holder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);

            holder.ll_div = (LinearLayout) convertView.findViewById(R.id.ll_div);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final LookWaybillData.DataBean data = getItem(position);

        BitmapUtil.showImgForNetPath(data.getUserImage(), holder.iv_header);
        holder.tv_driver_name.setText(data.getDriverName());
        if ("2".equals(data.getFleetType() )) {//2车 3船
            holder.tv_car_band.setText("牌");
        } else {
            holder.tv_car_band.setText("助");
        }
        holder.tv_car_band_num.setText(data.getPlateNumber());
        holder.tv_waibil_no.setText(data.getWaybillNo());
        holder.tv_weight.setText(StringUtil.getDouble(data.getWeight()+"") + "t");

        String unit="¥";
        if (currency!=null && "2".equals(currency)){
            unit="$";
        }else {
            unit="¥";
        }
        holder.tv_money.setText(StringUtil.getDouble(data.getSumMoney()+"") + unit);
        holder.ll_div.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, WaybilDetailActivity.class);
                intent.putExtra("waybilId", data.getWaybillId() + "");
                intent.putExtra("type", 2);
                activity.startActivity(intent);
            }
        });

        return convertView;
    }

    class ViewHolder {
        ImageView iv_header;
        TextView tv_driver_name;
        TextView tv_car_band;
        TextView tv_car_band_num;
        TextView tv_waibil_no;
        TextView tv_weight;
//        ImageView iv_choose;
//        TextView tv_reassign;
//        TextView tv_disagree;
//        TextView tv_delete;
        LinearLayout ll_div;


        TextView   tv_money;

    }


    private void deleteWaybil(final int applyNoStatus, int waybilId) {// 	2 拒绝 3 同意（删除运单并更新订单的相关数据）
        //先删除，后重新分配。
        DialogUtil.showLoadingDialog(activity, "申请中...");
        Map<String, Object> params = new HashMap<String, Object>();
        ArrayList<String> waybillIds=new ArrayList<>();
        waybillIds.add(waybilId+"");
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("waybillIds", waybillIds);
        params.put("applyNoStatus", applyNoStatus);

        MyHttpUtil.deletWaybil(activity, params, new MyCallback<ResultGoodsAllot>(activity, new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activity, "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(ResultGoodsAllot result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                   if (result.getMsg()!=null){
                       PrintUtil.toast(activity,result.getMsg());
                   }else {
                       PrintUtil.toast(activity,"操作成功");
                   }


                    EventBusUtil.postEvent(new RefleshFragEvent(5));
                } else {
                    PrintUtil.toast(activity, result.getMsg());
                }
            }
        });
    }
}
